﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.IO;

namespace newTurtle8
{
    public delegate void SoldierAddedEventHandler(object sender, EventArgs e);
    public delegate void SoldierRemovedEventHandler(object sender, EventArgs e);
    public delegate void SoldierUpdatedEventHandler(object sender, EventArgs e);
    internal class Soldier
    {
        
        public enum Shoe_Status
        {
            [HebrewName("יש")]Has,
            [HebrewName("אין - נשפט ומחכה לנעליים")]HasntAlreadySentenced,
            [HebrewName("אין - טרם נשפט")]HasntWaitToSentence,
            [HebrewName("לא ידוע")]Unknown 
        };
        public enum Couple_Status 
        { 
            [HebrewName("נשוי")]Married,
            [HebrewName("גרוש")]Divorced,
            [HebrewName("מאורס")]Engaged,
            [HebrewName("רווק")]Single,
            [HebrewName("לא ידוע")]Unknown 
        };
        public enum IdDisc_Status
        {
            [HebrewName("יש את שניהם")]HaveBoth,
            [HebrewName("רק נעליים")]ShoesOnly,
            [HebrewName("רק צוואר")]NeckOnly,
            [HebrewName("אין")]DontHave,
            [HebrewName("לא ידוע")]Unknown 
        };
        public enum Relation
        {
            [HebrewName("אמא")]Mother,
            [HebrewName("אבא")]Father,
            [HebrewName("סבא")]GrandFather,
            [HebrewName("סבתא")]GrandMother,
            [HebrewName("אח")]Brother,
            [HebrewName("אחות")]Sister,
            [HebrewName("חבר/ה")]Friend,
            [HebrewName("ילד/ה")]Child,
            [HebrewName("אשה")]Wife,
            [HebrewName("לא ידוע")]Unknown 
        };
        #region propeties

        //PersonalData
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string SocialID { get; set; }
        public string ArmyID { get; set; }
        public string HomePhone { get; set; }
        public string CellPhone { get; set; }
        public string Email { get; set; }
        public DateTime Birthday { get; set; }
        public string City { get; set; }
        public string Street { get; set; }
        public short BuildingNum { get; set; }
        public short ApartmentNum { get; set; }
        public Image Picture { get; set; }
        public string comments { get; set; }

        //FamilyData
        public Couple_Status CoupleStatus { get; set; }
        public string WifeName { get; set; }
        public byte ChildrenCtr { get; set; }
        public string ChildrenNames { get; set; }

        //armyData
        public string Position { get; set; }
        public List<string> QualifiedList { get; set; }
        public DateTime ArrivalDate{ get; set; }
        public byte Platoon{ get; set; }
        public byte ShoeSize{ get; set; }
        public Shoe_Status ShoeStatus{ get; set; }
        public IdDisc_Status IdDiscStatus{ get; set; }
        public bool IsActive{ get; set; }

        //ContactData
        public string ContactFirstName{ get; set; }
        public string ContactLastName{ get; set; }
        public string contactCellPhone{ get; set; }
        public string ContactHomePhone{ get; set; }
        public string ContactEmail{ get; set; }
        public string ContactAdress{ get; set; }
        public Relation ContactRelation {get; set;}
      
        #endregion

        public Soldier()
        {
            //default settings
            CoupleStatus = Couple_Status.Unknown;
            ShoeStatus = Shoe_Status.Unknown;
            IdDiscStatus = IdDisc_Status.Unknown;
            ContactRelation = Relation.Unknown;

            QualifiedList = new List<string>();
        }
        public Soldier(string armyID)
            : this()
        {
            SqlCommand selectCmd = SqlManager.SqlCommandFactory(new FileInfo(@"SELECT\SoldierData.SELECT.sql"));
            selectCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            DataTable dt = SqlManager.selectSqlQueryDataTable(selectCmd);

            //if == 1 means there is a soldier with this armyID
            if (dt.Rows.Count == 1)
            {
                DataRow row = dt.Rows[0];

                //PersonalData
                ArmyID = SqlManager.cast<string>(row, "armyID");
                SocialID = SqlManager.cast<string>(row, "socialID");
                FirstName = SqlManager.cast<string>(row, "firstName");
                LastName = SqlManager.cast<string>(row, "lastName");
                HomePhone = SqlManager.cast<string>(row, "homePhone");
                CellPhone = SqlManager.cast<string>(row, "cellPhone");
                Email = SqlManager.cast<string>(row, "email");
                Birthday = SqlManager.cast<DateTime>(row, "birthday");
                HomePhone = SqlManager.cast<string>(row, "homePhone");
                City = SqlManager.cast<string>(row, "city");
                Street = SqlManager.cast<string>(row, "street");
                BuildingNum = SqlManager.cast<short>(row, "buildingNum");
                ApartmentNum = SqlManager.cast<short>(row, "apartmentNum");
                Picture = CompanyManager.byteArrayToImage(SqlManager.cast<byte[]>(row, "picture"));
                comments = SqlManager.cast<string>(row, "comments");

                //FamilyInfo
                CoupleStatus = (Soldier.Couple_Status)EnumUtils.enumValueOf(SqlManager.cast<string>(row, "CoupleStatus"), typeof(Soldier.Couple_Status));
                WifeName = SqlManager.cast<string>(row, "wifeName");
                ChildrenCtr = SqlManager.cast<byte>(row, "childrenCtr");
                ChildrenNames = SqlManager.cast<string>(row, "childrenNames");

                //ContactInfo
                ContactFirstName = SqlManager.cast<string>(row, "ContactFirstName");
                ContactLastName = SqlManager.cast<string>(row, "ContactLastName");
                ContactHomePhone = SqlManager.cast<string>(row, "ContactHomePhone");
                contactCellPhone = SqlManager.cast<string>(row, "ContactCellPhone");
                ContactEmail = SqlManager.cast<string>(row, "ContactEmail");
                ContactRelation = (Soldier.Relation)EnumUtils.enumValueOf(SqlManager.cast<string>(row, "ContactRelation"), typeof(Soldier.Relation)); 
                ContactAdress = SqlManager.cast<string>(row, "ContactAdress");

                //ArmyInfo
                Position = SqlManager.cast<string>(row, "position");
                ArrivalDate = SqlManager.cast<DateTime>(row, "arrivalDate");
                Platoon = SqlManager.cast<byte>(row, "platoon");
                ShoeSize = SqlManager.cast<byte>(row, "shoeSize");
                ShoeStatus = (Soldier.Shoe_Status)EnumUtils.enumValueOf(SqlManager.cast<string>(row, "shoeStatus"), typeof(Soldier.Shoe_Status)); 
                IdDiscStatus = (Soldier.IdDisc_Status)EnumUtils.enumValueOf(SqlManager.cast<string>(row, "idDiscStatus"), typeof(Soldier.IdDisc_Status)); 
                IsActive = SqlManager.cast<bool>(row, "isActive");

                //Qualifications
                selectCmd = SqlManager.SqlCommandFactory(new FileInfo(@"SELECT\SoldierQualifications.SELECT.sql"));
                selectCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
                dt = SqlManager.selectSqlQueryDataTable(selectCmd);

                foreach (DataRow r in dt.Rows)
                    QualifiedList.Add((string)r["qualification"]);
            }
        }        
        public static bool isExists(string armyID)
        {
            string selectStr = @"SELECT COUNT(armyID) FROM PersonalData WHERE armyID=@armyID";
            SqlCommand selectCmd = SqlManager.SqlCommandFactory(selectStr);
            selectCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            if(Convert.ToInt32(SqlManager.executeSqlScalar(selectCmd)) == 0)
                return false;
            return true;
        }
        public static void addYamam(string armyID, DateTime mofaDate, DateTime date)
        {
            SqlCommand insertCmd = SqlManager.getSqlCommandFromFile(@"\INSERT\Yamam.INSERT.sql");
            insertCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = mofaDate;
            insertCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            insertCmd.Parameters.Add("@date", SqlDbType.Date).Value = date;

            SqlManager.executeNonQuery(insertCmd);
        }
        public static void removeYamam(string armyID, DateTime mofaDate, DateTime date)
        {
            SqlCommand deleteCmd = SqlManager.getSqlCommandFromFile(@"\DELETE\Yamam.DELETE.sql");
            deleteCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            deleteCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = mofaDate;
            deleteCmd.Parameters.Add("@date", SqlDbType.Date).Value = date;

            SqlManager.executeNonQuery(deleteCmd);
        }
    }

    
}
